/*
  Created on Mar 20, 2006
  (C) 2005 
  @author Ben Freiberg

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

package snobject;

import snbenchlib.Util;
import snobject.snObject;

public class snDouble extends snObject {

	/**
	 * instantiate a new snDouble with given content
	 * @param _content
	 */
	public snDouble(double i){
		super();
		
		content = new Double(i);
	}
	
	public snDouble(Double i){
		content = i;
	}
	
	public String getXmlMimeType() { return MimeTypes.DOUBLE; }
	public String getWebMimeType() { return "text/plain"; }
	public String getBinaryMimeType() { return "application/binary"; }
	
	public Double getDouble(){
		return (Double)this.content;
	}
	
	public double getDbl(){
		return getDouble().doubleValue();
	}

	public void xmlWrite(java.io.OutputStream out,int depth) throws java.io.IOException {
		xmlWritePre(out,depth);
		out.write(((Double)this.content).toString().getBytes());
		xmlWritePost(out,0);
	}

	public void webWrite(java.io.OutputStream out, int depth) throws java.io.IOException {
		step.Node.Indent(out, depth);
		out.write(((Double)content).toString().getBytes());
	}

	public void binaryWrite(java.io.OutputStream out) throws java.io.IOException {
//		double w = ((Double)content).doubleValue();
//		/* network byte order */
//		for (int i=0 ; i<4 ; i++) {
//			out.write((w & 0xff000000)/0x1000000);
//			w <<= 8;
//		}
	}

	public static snDouble xmlParse(org.w3c.dom.Element e) throws step.parse.SyntaxError {
		if (!e.getTagName().equals("snobject"))
			return null;
		String z = "";
		for (org.w3c.dom.Node n = e.getFirstChild() ; 
		     n != null ; 
		     n = n.getNextSibling()) {
			if (n.getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
				org.w3c.dom.Text t = (org.w3c.dom.Text) n;
				z += t.getData();
			}
		}
		z = z.trim();
		try {
			return new snDouble(new Double(z));
		} catch (NumberFormatException ex) {
			throw new step.parse.SyntaxError("Non-double string in snbench/double: [" + z + "]");
		}
	}
}